//
//  CJSStoryViewController.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/24.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSStoryViewController.h"
#import "CJSStoryHeadView.h"
#import "CJSStoryCell.h"
#import "CJSDetail_list.h"
#import "Resources.h"
#import "CJSStroy.h"
#import "User.h"
#import "WSTools.h"
#import "CJSFootView.h"
#import "MBProgressHUD.h"

@interface CJSStoryViewController ()<UITableViewDataSource, UITableViewDelegate,MBProgressHUDDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) CJSStoryHeadView *headView;

@property (nonatomic, retain) CJSStroy *story;

@property (nonatomic, retain) CJSFootView *footView;

@property (nonatomic, retain) MBProgressHUD *mbBarPrpgressV;

@property (nonatomic, retain) UIImage *image;

@end

@implementation CJSStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CJSStoryCell class] forCellReuseIdentifier:@"identify"];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.footView = [[CJSFootView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    self.footView.backgroundColor = self.tableView.backgroundColor;
    self.tableView.tableFooterView = self.footView;
    
    self.mbBarPrpgressV = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [self.mbBarPrpgressV show:YES];
    
    [self AFNWorking];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.story.detailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify"];
    
    CJSDetail_list *detail_list = [self.story.detailArray objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.detail_list = detail_list;
    
    if (indexPath.row == 0) {
        self.image = [cell getImage];
    }
    cell.contentView.backgroundColor = self.tableView.backgroundColor;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSDetail_list *detailL = [self.story.detailArray objectAtIndex:indexPath.row];
    
    return 40 + (SCREEN_WIDTH * detailL.photo_height / detailL.photo_width) + [WSTools heightForContent:[[self.story.detailArray objectAtIndex:indexPath.row] text] size:15];
}

- (void)AFNWorking {
    
    NSString *strUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", self.spot_id];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSDictionary *bigDic = result;
        self.story = [[CJSStroy alloc] init];
        
        NSDictionary *spot = [[bigDic objectForKey:@"data"] objectForKey:@"spot"];
        [self.story setValuesForKeysWithDictionary:spot];
        
        NSDictionary *trip = [[bigDic objectForKey:@"data"] objectForKey:@"trip"];
        [self.story.users setValuesForKeysWithDictionary:[trip objectForKey:@"user"]];
        self.footView.time = [[[self.story detailArray] firstObject] photo_date_created];
        [self createHeaderView];
        [self.tableView reloadData];
        [self.mbBarPrpgressV hide:YES];
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.mbBarPrpgressV hide:YES];
            [self.mbBarPrpgressV hide:NO];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

- (void)createHeaderView {
    
    //头视图***********
    
    CGFloat height = [WSTools heightForContent:self.story.text size:20] + 40;
    self.headView = [[CJSStoryHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height + 180)];
    self.headView.backgroundColor = self.tableView.backgroundColor;
    self.headView.story = self.story;
    self.tableView.tableHeaderView = self.headView;
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
